feature: add StatsCollector for basic metrics aggregation#16
Open
feature: add StatsCollector for basic metrics aggregation#16
Conversation
devcrocod
approved these changes
Mar 20, 2026
| private final LongAccumulator maxLatencyMs = new LongAccumulator(Math::max, Long.MIN_VALUE); | ||
| private final LongAccumulator minLatencyMs = new LongAccumulator(Math::min, Long.MAX_VALUE); | ||
|
|
||
| public void record(int statusCode, long latencyMs) throws IllegalArgumentException { |
Collaborator
There was a problem hiding this comment.
Please keep in mind that this code is not thread-safe, so errorCount may end up becoming negative
| private final LongAccumulator maxLatencyMs = new LongAccumulator(Math::max, Long.MIN_VALUE); | ||
| private final LongAccumulator minLatencyMs = new LongAccumulator(Math::min, Long.MAX_VALUE); | ||
|
|
||
| public void record(int statusCode, long latencyMs) throws IllegalArgumentException { |
Collaborator
There was a problem hiding this comment.
IllegalArgumentException is unchecked exception
|
|
||
| totalRequests.increment(); | ||
| if (200 <= statusCode && statusCode < 300) { | ||
| successCount.increment(); |
Collaborator
There was a problem hiding this comment.
Are 1xx or 3xx statuses as errors?
Comment on lines
+30
to
+55
| public StatsSnapshot getSnapshot() { | ||
| long snapshotTotalRequests = totalRequests.sum(); | ||
| long snapshotSuccessCount = successCount.sum(); | ||
| long snapshotTotalLatencyMs = totalLatencyMs.sum(); | ||
|
|
||
| long snapshotMinLatencyMs = (snapshotTotalRequests == 0 ? 0 : minLatencyMs.get()); | ||
| long snapshotMaxLatencyMs = (snapshotTotalRequests == 0 ? 0 : maxLatencyMs.get()); | ||
| long snapshotAvgLatencyMs = | ||
| (snapshotTotalRequests == 0 ? 0 : snapshotTotalLatencyMs / snapshotTotalRequests); | ||
|
|
||
| return new StatsSnapshot( | ||
| snapshotTotalRequests, | ||
| snapshotSuccessCount, | ||
| snapshotTotalRequests - snapshotSuccessCount, | ||
| snapshotAvgLatencyMs, | ||
| snapshotMinLatencyMs, | ||
| snapshotMaxLatencyMs); | ||
| } | ||
|
|
||
| public void reset() { | ||
| totalRequests.reset(); | ||
| successCount.reset(); | ||
| totalLatencyMs.reset(); | ||
| minLatencyMs.reset(); | ||
| maxLatencyMs.reset(); | ||
| } |
Collaborator
There was a problem hiding this comment.
add method getSnapshotAndReset()
| long snapshotMinLatencyMs = (snapshotTotalRequests == 0 ? 0 : minLatencyMs.get()); | ||
| long snapshotMaxLatencyMs = (snapshotTotalRequests == 0 ? 0 : maxLatencyMs.get()); | ||
| long snapshotAvgLatencyMs = | ||
| (snapshotTotalRequests == 0 ? 0 : snapshotTotalLatencyMs / snapshotTotalRequests); |
Collaborator
There was a problem hiding this comment.
Try to use double-precision arithmetic where possible. Integer arithmetic can introduce significant error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Thread-safe StatsCollector for aggregating per-request metrics.
Collects status codes and latencies, provides snapshot with
total/success/error counts, avg/min/max latency.
Uses LongAdder and LongAccumulator for lock-free concurrency.
How to verify
./mvnw test
Closes #11